<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目名称" prop="postName">
        <el-input
          v-model="queryParams.quesName"
          placeholder="请输入题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目类型" prop="quesType">
        <el-input
          v-model="queryParams.quesType"
          placeholder="请输入题目类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:ques:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:ques:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:ques:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:ques:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="quesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目编号" align="center" prop="quesId" />
      <el-table-column label="题目名称" align="center" prop="quesName" />
      <el-table-column label="所属题库" align="center" prop="stuQuestionStore.storeName"/>
zs
      <el-table-column label="题目类型" align="center" prop="quesType">
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            type=""
            disable-transitions>{{scope.row.quesType}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="题目难度" align="center" prop="quesHard">
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            type="info"
            disable-transitions>{{scope.row.quesHard}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:ques:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:ques:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题目名称" prop="quesName">
          <el-input v-model="form.quesName" placeholder="请输入题干名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属题库">
              <el-select v-model="form.storeId"  placeholder="请选择题库">
                <el-option
                  v-for="item in storeOptions"
                  :key="item.storeId"
                  :label="item.storeName"
                  :value="item.storeId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="题目难度" prop="quesHard">
              <el-select v-model="form.quesHard"  placeholder="请选择难度">
                <el-option label="简单" value="简单"></el-option>
                <el-option label="中等" value="中等"></el-option>
                <el-option label="困难" value="困难"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="quesType" >
              <el-select v-model="form.quesType"  placeholder="请选择题目类型"
                         @change="typechange" >
                <el-option label="单选题" value="单选题"></el-option>
                <el-option label="多选题" value="多选题"></el-option>
                <el-option label="判断题" value="判断题"></el-option>
                <el-option label="简答题" value="简答题"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="typeSelect=='判断题'" label="答案" prop="answer">
              <el-switch
                v-model="form.answer"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="正确"
                inactive-text="错误"
              >
              </el-switch>
            </el-form-item>
            <el-form-item v-if="typeSelect=='单选题'" label = "答案" prop ="answer">
              <el-select  v-if="typeSelect=='单选题'" v-model="form.answer"
                          placeholder="请选择单选题答案">
                <el-option label = "A" value="A"></el-option>
                <el-option label = "B" value="B"></el-option>
                <el-option label = "C" value="C"></el-option>
                <el-option label = "D" value="D"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item v-if="typeSelect=='多选题'" label = "答案" prop = "manyAnswer">
              <el-select  v-if="typeSelect=='多选题'"  v-model="form.manyAnswer"
                          multiple placeholder="请选择多选题答案">
                <el-option label = "A" value="A"></el-option>
                <el-option label = "B" value="B"></el-option>
                <el-option label = "C" value="C"></el-option>
                <el-option label = "D" value="D"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item v-if="typeSelect=='单选题' || typeSelect == '多选题'" label = "选项A" prop = "selContA" >
              <editor v-model="form.selContA" :min-height="160"/>
            </el-form-item>

            <el-form-item v-if="typeSelect=='单选题' || typeSelect == '多选题'" label = "选项B" prop = "selContB">
              <editor v-model="form.selContB" :min-height="160"/>
            </el-form-item>

            <el-form-item v-if="typeSelect=='单选题' || typeSelect == '多选题' " label = "选项C" prop = "selContC">
              <editor v-model="form.selContC" :min-height="160"/>
            </el-form-item>

            <el-form-item v-if="typeSelect=='单选题' || typeSelect == '多选题'" label = "选项D" prop = "selContD">
              <editor v-model="form.selContD" :min-height="160"/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listQues, getQues, delQues, addQues, updateQues } from "@/api/store/ques";
import {delStage, getStage} from "@/api/stage/stagelist";

export default {
  name: "Ques",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //阶段名称
      quesId:undefined,
      quesName:undefined,
      quesHard:undefined,
      storeOptions:[],
      typeOptions:[],
      typeSelect:"",
      quesOption:'',
      answer:'',
      manyAnswer:[],
      jugAnswer:'',
      selContA:'',
      selContB:'',
      selContC:'',
      selContD:'',
      quesType:undefined,
      typeName:undefined,
      // 总条数
      total: 0,
      // 岗位表格数据
      quesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        quesId: undefined,
        quesName: undefined,
        quesType:undefined,
        quesHard:undefined,
        storeId:undefined,
        selContA:'',
        selContB:'',
        selContC:'',
        selContD:'',
        storeName:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        quesName: [
          { required: true, message: "题库名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 显示所有 */
    getList() {
      this.loading = true;
      listQues(this.queryParams).then((response) => {
        this.quesList = response.rows;
        this.total = response.total;
        this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        quesId: undefined,
        storeId:undefined,
        quesName: undefined,
        quesHard:undefined,
        quesType:undefined,
        selContA:'',
        selContB:'',
        selContC:'',
        selContD:'',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getQues().then(response => {
        this.storeOptions = response.stores;
        this.open = true;
        this.title = "添加题目";
      });
    },

    /**显示不同题目的ti干*/
    typechange(val){
      this.typeSelect = val;
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const quesId = row.quesId || this.ids
      getQues(quesId).then(response => {
        this.storeOptions = response.stores;
        this.form = response.data;
        this.open = true;
        this.title = "修改题目";
        this.typeSelect = response.data.quesType
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.quesId != undefined) {
            updateQues(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            addQues(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const quesIds = row.quesId || this.ids;
      this.$modal.confirm('是否确认删除题目编号为"' + quesIds + '"的数据项？').then(function() {
        return delQues(quesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.diaolog .el-table .el-table__header-wrapper thead th,
.diaolog .el-table .el-table__fixed-header-wrapper thead th {
  display: none;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
